var appPath = getAppPath();

$(function(){
	// 来源
	$("#queryJgFrom").combobox({
		url:appPath+'/itbCom/getJgFrom4Combo.action?blkFlg=1',
		valueField:'from_value',
		textField:'from_text',
		onSelect:function(row){
			// TODO
		}
	});
	
	initJspGrid();
});

// 样版列表初始化
function initJspGrid(){
	
	$('#priceGrid').datagrid({
	    url:appPath + "/itbPrice/getPriceInfoPg.action",
	    height:'full',
	    //remoteSort:false,
	    striped:true,
	    fitColumns:true,
	    queryParams: getQueryParam(),
	    singleSelect:false,
	    // If set to true, clicking a checkbox will always select the row. 
	    // If false, selecting a row will not check the checkbox.
	    selectOnCheck:true,
	    // If true, the checkbox is checked/unchecked when the user clicks on a row. 
	    // If false, the checkbox is only checked/unchecked when the user clicks exactly on the checkbox.
	    checkOnSelect:true,
	    frozenColumns:[[
	        {field:'ck',checkbox:'true',width:10,align:'center',sortable:false}
	    ]],
	    columns:[[
  	        {field:'price_id',title:'价格ID',width:60,align:'center',sortable:false},
  	        {field:'pp_id',title:'套餐ID',width:60,align:'center',sortable:false,
	        	formatter:function(value, rowData, rowIndex){
	        		return '<span style="color:red">'+value+'</span>';
	        	}
		    },
  	        {field:'price',title:'价格详情',width:80,align:'right',sortable:false,
	        	formatter:function(value, rowData, rowIndex){
	        		if(!rowData.price_id) return "N/A";
	        		var show = Number(value||'0')/100;
	        		return "<a href='javascript:void(0)' onClick='showDetail("+rowData.price_id+")'>"+show+" 元</a>";
	        	}
		    },
  	        {field:'unit',title:'计量单位',width:80,align:'center',sortable:false},
  	        {field:'min',title:'数量最小值',width:80,align:'center',sortable:false},
  	        {field:'max',title:'数量最大值',width:80,align:'center',sortable:false},
  	        {field:'show',title:'数量显示值',width:80,align:'center',sortable:false},
  	        {field:'desc',title:'备注',width:140,align:'left',sortable:false},
  	        {field:'state',title:'状态',width:50,align:'center',sortable:false,
  	        	formatter:function(value, rowData, rowIndex){
	        		if(!rowData.price_id) return "N/A";
	        		var show = getState4Show(value);
	        		return "<a href='javascript:void(0)' onClick='editState("+rowData.price_id+")'>"+show+"</a>";
	        	}
		    }, 
	        {field:'create_time',title:'创建时间',width:70,align:'center',sortable:false},
	        {field:'update_time',title:'更新时间',width:70,align:'center',sortable:false}
	    ]],
	    hideColumn:['user_type'],
	    //view: imgView,// 自定义View
	    pagination:true,
		rownumbers:true,
		toolbar:[
			/*{
				text:'删除商品',
				iconCls:'icon-cancel',
				handler:function(){
					delPrice();
				}
			}*/
			/*{
				text:'新建价格信息',
				iconCls:'icon-add',
				handler:function(){
					addPrice();
				}
			}*/		
		]
	});
}

function getState4Show(value){
	var state = '<span style="color:red">未知</span>';
	switch(Number(value||'0')){
		case 10: state="有效"; break;
		case 20: state="无效"; break;
	}
	return state;
}


//新建价格信息
/*function addPrice(){
	$("#editTable").show();
	$('#editForm').form('clear');
	$('#editPpId').html('');
	$("#editJgPrint").val('');
	$("#editJgPrice").val('');
	
	$('#editJgUnit').val('');
	$("#editJgMax").val('');
	$("#editJgMin").val('');
	$("#editJgShow").val('');

	
	$("#editBtn").show();
	$('#editDlg').window({
		title:'新建价格',
		iconCls:'icon-edit',
		modal: true,
		shadow: true,
		collapsible:false,
		minimizable:false,
		maximizable:false
	});
	
	$('#editDlg').window('move',{top:20,left:20});
	$('#editDlg').window('open');
}
*/
// 编辑商品信息
function doEdit(){
	var jgPrice = $('#editJgPrice').val();
	var jgId = $("#editJgId").val();
	//var jgNo = $("#editJgNo").val();
	var jgUnit = $("#editJgUnit").val();	//	计量单位
	var jgMax = $("#editJgMax").val();		//	数量最大值	
	var jgMin =  $('#editJgMin').val();		//	数量最小值
	var jgShow =  $('#editJgShow').val();	//	数量显示值

	if(jgId && jgId.length>0
		&&  jgPrice && jgPrice.length>0
		&&  jgUnit && jgUnit.length>0
		&&  jgMax && jgMax.length>0
		&&	jgMin && jgMin.length>0
		&&	jgShow && jgShow.length>0) {
		// 默认新建
		var url = appPath+"/itbPriceWeb/addPrice.action";
		if(Number(jgId||'0')>0) {
			// 更新
			url = appPath+"/itbPriceWeb/updPrice.action";
		}
		var sendData = {
				
			"priceInfo.price_id": jgId||"",
			"priceInfo.price": jgPrice,
			
			"priceInfo.unit": jgUnit,			
			"priceInfo.max": jgMax,
			"priceInfo.min": jgMin,
			"priceInfo.show": jgShow,			
		};
	    $.post(url, sendData, function(data){  
			if (data == "ok") { 
				$.messager.alert('提示信息','提交成功','info');
				$('#editDlg').window('close');
				doQuery();
	        } else {
	        	$.messager.alert('提示信息','提交失败','info');
	        }
	    });
		
	} 
	else {
		$.messager.alert('提示信息','请填写价格信息／计量单位／数量','info');
	}
}

function cancelEdit(){
	$('#editDlg').window('close');
}


//添加、编辑价格信息
function showDetail(jgId){
	$("#editTable").show();
	$('#editForm').form('clear');
	$("#editJgId").val(jgId);
	$("#editJgNo").html('');
	
	// 加载数据
	var url = appPath + "/itbPrice/getPriceInfoLst.action?price_id="+ jgId;
	$.get(url,function(ret){
		if (ret && ret.length>0){
			var info = ret[0];
			$("#editJgPrice").val(info.price||'');
			$("#editJgNo").html(info.price_id||'');
			$("#editJgUnit").val(info.unit||'');
			$("#editJgMax").val(info.max||'');
			$("#editJgMin").val(info.min||'');
			$("#editJgShow").val(info.show||'');		
			$("#editJgDesc").val(info.desc||'');
		}
	});
	
	$("#editBtn").show();
	$('#editDlg').window({
		title:'价格详情/编辑',
		iconCls:'icon-edit',
		modal: true,
		shadow: true,
		collapsible:false,
		minimizable:false,
		maximizable:false
	});
	
	$('#editDlg').window('move',{top:20,left:20});
	$('#editDlg').window('open');
}



//价格状态   编辑
function editState(jgId){
	$("#stateDlg").show();
	$('#stateForm').form('clear');
	$("#updJgNo").html('');

	$("#updJgId").val(jgId);
	
	var title = '价格参数状态';
	var url = appPath + "/itbPrice/getPriceInfoLst.action?price_id="+ jgId;
	$.get(url,function(ret){
		if (ret && ret.length>0){
			var info = ret[0];
			$("#updJgNo").html(info.price_id||'');
			$("#updJgState").combobox('select',info.state||0);
		}
	});
	
	$("#stateBtn").show();
	$('#stateDlg').window({
		title: title,
		iconCls:'icon-edit',
		modal: true,
		shadow: true,
		collapsible:false,
		minimizable:false,
		maximizable:false
	});
	
	$('#stateDlg').window('move',{top:20,left:20});
	$('#stateDlg').window('open');
}
function cancelUpdState(){
	$('#stateDlg').window('close');
}
function doUpdState(){
	var id = $("#updJgId").val();
	var state = $("#updJgState").combobox('getValue');
	
	// 默认更新订单
	var url = appPath+"/itbPriceWeb/updPrice.action";
	var sendData = {
		"priceInfo.price_id": Number(id||"0"),
		"priceInfo.state": Number(state || "0"),
	};
	
  $.post(url, sendData, function(data){  
		if (data == "ok") { 
			$.messager.alert('提示信息','更新成功','info');
			$('#stateDlg').window('close');
			doQuery();			
      } else {
      	$.messager.alert('提示信息','更新失败','info');
      }
  });
}



function cancelUpdJg(){
	$('#stateDlg').window('close');
}


function delPrice(){
	var selectPrice=$("#priceGrid").datagrid("getChecked");
	if(!selectPrice || selectPrice.length == 0){
		$.messager.alert('提示信息','请选择价格项目！','warning');
		return;
	}
	for(var i=0; i<selectPrice.length; i++){
		if(Number(selectPrice[i].state||'0')==10){
			$.messager.alert('提示信息','不能删除[有效]价格项目！','warning');
			return;
		}
	}
	
	$.messager.confirm("确认", "是否确认[删除]所选价格项目？",
		function(r){
	        if (r) { 
				var ids = "";
				$.each(selectPrice, function(index, item){
					ids = ids + item.price_id + ",";
				});
				var url = appPath+"/itbPriceWeb/delPrice.action?price_id="+ids;
				$.post(url,function(data){
					if(data=="ok"){
						$.messager.alert('提示信息','删除成功','info');
						doQuery();
					}else{
						$.messager.alert('提示信息','删除失败','info');
					}
				});
	        }
		}
	);
}

/**
 * 获取请求信息
 */
function getQueryParam(){
	var queryObject = {};
	var findState = $("#queryJgState").combobox('getValue');			//价格状态
	
	var findShow = $("#queryJgShow").val();								//数量显示值
	
	if(findState && Number(findState||'0')>0) queryObject.findJgState = findState;	// 状态
	if(findShow && findShow.length>0) queryObject.findJgShow = findShow;					// 数量显示值
	return queryObject;
}

/**
 * 查询数据
 */
function doQuery(){
	$("#priceGrid").datagrid('unselectAll');
	$("#priceGrid").datagrid('load', getQueryParam());
}

function fmtCreateTime(value, rowData, rowIndex){
	if(!value)return "";
	var s = new Date(value);
	return rowData.create_time=$.DateUtil.format(s,'yyyy/MM/dd hh:mm:ss');
}
